/*!
 * \file jxbankafanatp_header.c
 *
 * \brief JXBANKAFANATP消息头操作
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.
 *
 * \version 1.0
 * \author songbb
 * \date 2017/04/20
 *
 */

#include <stdio.h>
#include <ctype.h>

#include "frct_comm.h"

#include "jxbankafanatp_comm.h"
#include "jxbankafanatp_header.h"
#include "config.h"

int jxbankafanatp_header_detect(char *p_payload
                             , unsigned int ui_payload_len)
{
    JXBANKAFANATP_PRINT_INFO("jxbankafanatp_header_detect...\n");

    if (ui_payload_len < APM_JXBANKAFANATP_HDR_LEN)
        goto unknown;

    // 校验Reserved字段
    if (memcmp(p_payload + 4, "\x01\x00\x00\x00\x00\x01", 6) != 0)
        goto unknown;

    // 校验Version字段
    if (*(p_payload + 10) != 0x10 && *(p_payload + 10) != 0x30)
        goto unknown;

    return APM_L7_IS_YES;
unknown:
    return APM_L7_IS_UNKNOWN;
}

int jxbankafanatp_header_unpack(char *p_payload
                            , unsigned int ui_payload_len
                            , Jxbankafanatp_header *p_st_jxbankafanatp_header)
{
    unsigned int *p_ui_pkt_len;
    unsigned int ui_pkt_len;

    if (ui_payload_len < APM_JXBANKAFANATP_HDR_LEN)
        goto error;

    p_ui_pkt_len = (unsigned int *) p_payload;
    ui_pkt_len = (unsigned int) ntohl(*p_ui_pkt_len);

    p_st_jxbankafanatp_header->ui_pkt_len = ui_pkt_len + 4;

    return FR_SUCC;
error:
    return FR_FAIL;
}
